package cn.iocoder.yudao.module.paymember.mq.consumer.user;

import cn.iocoder.yudao.framework.common.constant.BizConstant;
import cn.iocoder.yudao.module.member.controller.admin.userpointrecords.vo.UserPointRecordsSaveReqVO;
import cn.iocoder.yudao.module.member.enums.userpointrecords.UserPointRecordsBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.userpointrecords.UserPointRecordsOpTypeEnum;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.service.userpointrecords.UserPointRecordsService;
import cn.iocoder.yudao.module.pay.dal.dataobject.packagec.PackageDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.packageorder.PackageOrderDO;
import cn.iocoder.yudao.module.pay.mq.message.packageorder.PayPackageOrderSuccessMessage;
import cn.iocoder.yudao.module.pay.service.packagec.PackageService;
import cn.iocoder.yudao.module.pay.service.packageorder.PackageOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 套餐订单支付成功，更新会员用户积分的消费者
 */
@Slf4j
@Component
public class MemberUserPointsPayPackageOrderSuccessConsumer {

    @Resource
    private PackageOrderService packageOrderService;
    @Resource
    private PackageService packageService;
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private UserPointRecordsService userPointRecordsService;

    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    @EventListener
    public void onMessage(PayPackageOrderSuccessMessage message) {
        log.info("[onMessage][消息内容({})]", message);
        Long merchantOrderId = Long.parseLong(message.getMerchantOrderId());
        PackageOrderDO packageOrderDO = packageOrderService.getPackageOrder(merchantOrderId);
        PackageDO packageDO = packageService.getPackage(packageOrderDO.getPackageId());
        Long userId = packageOrderDO.getUserId();
        Integer points = packageDO.getPoints();
        memberUserService.updateUserPoints(userId, points);
        addUserPointsRecords(userId, merchantOrderId, points);
    }

    private void addUserPointsRecords(Long userId, Long bizId, Integer points) {
        UserPointRecordsSaveReqVO userPointRecordsSaveReqVO = new UserPointRecordsSaveReqVO();
        userPointRecordsSaveReqVO.setUserId(userId);
        userPointRecordsSaveReqVO.setOpType(UserPointRecordsOpTypeEnum.ADD.getCode());
        userPointRecordsSaveReqVO.setBizType(UserPointRecordsBizTypeEnum.PACKAGE_ORDER_PAY.getType());
        userPointRecordsSaveReqVO.setBizId(bizId);
        userPointRecordsSaveReqVO.setPoints(points);
        userPointRecordsSaveReqVO.setTotalPoints(memberUserService.getUser(userId).getPoints());
        userPointRecordsSaveReqVO.setCreator(BizConstant.SYSTEM_USER_ID.toString());
        userPointRecordsService.createUserPointRecords(userPointRecordsSaveReqVO);
    }

}
